package com.itheima.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.pojo.Order;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.Map;

public interface OrderMapper extends BaseMapper<Order> {
    @Select("select count(*) from order_t where member_id = #{mid} and order_date = #{orderDate}")
    Integer hasSameOrderOrNot(@Param("mid") Long id,@Param("orderDate")String orderDate);

    @Select("select mt.name as member, st.name as setmeal, ot.order_type as orderType, ot.order_date as orderDate " +
            "from order_t ot, member_t mt, setmeal_t st \n" +
            "where ot.setmeal_id = st.id and ot.member_id = mt.id and ot.id=#{id}")
    Map findMSGById(@Param("id") Long id);

    //支付成功后，修改订单预约状态为成功预约（未支付0---->成功预约1）
    @Update("update order_t set pay_status = 1 where id = #{orderId}")
    void updateState(@Param("orderId") long orderId);

    //根据订单ID查询用户的支付状态
    @Select("select pay_status from order_t where id = #{id}")
    String findStatusById(@Param("id") Long id);

    //删除异常订单
    @Delete("delete from order_t where id = #{orderId}")
    void deleteOrderById(@Param("orderId") long orderId);

    //根据订单id查询预约日期
    @Select("select order_date from order_t where id = #{orderId}")
    String queryOrderDateById(@Param("orderId") long orderId);

    //根据预约日期，修改对应的预约人数
    @Update("update ordersetting_t " +
            "set reservations = reservations - 1 " +
            "where ordersetting_t.order_date = #{orderDate}")
    int updateOrderSettingByDate(@Param("orderDate") String orderDate);
}
